package com.matriq.cloudmanager;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.PersonalInfo;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.ExceptionAsyncTask;
import net.lgpage.util.Cleaner;

public class PersonInfoActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_person_info);
		setViews();
		setEvents();
		loadData();
	}

	LinearLayout ll[] = new LinearLayout[12];
	TextView tv[] = new TextView[11];
	LinearLayout viewActionBar;
	ImageView iv_head;
	TextView tv_title;
	ImageButton ll_back;

	private void setViews() {
		ll[0] = (LinearLayout) this.findViewById(R.id.ll0);
		ll[1] = (LinearLayout) this.findViewById(R.id.ll1);
		ll[2] = (LinearLayout) this.findViewById(R.id.ll2);
		ll[3] = (LinearLayout) this.findViewById(R.id.ll3);
		ll[4] = (LinearLayout) this.findViewById(R.id.ll4);
		ll[5] = (LinearLayout) this.findViewById(R.id.ll5);
		ll[6] = (LinearLayout) this.findViewById(R.id.ll6);
		ll[7] = (LinearLayout) this.findViewById(R.id.ll7);
		ll[8] = (LinearLayout) this.findViewById(R.id.ll8);
		ll[9] = (LinearLayout) this.findViewById(R.id.ll9);
		ll[10] = (LinearLayout) this.findViewById(R.id.ll10);
		ll[11] = (LinearLayout) this.findViewById(R.id.ll11);
		tv[0] = (TextView) this.findViewById(R.id.tv0);
		tv[1] = (TextView) this.findViewById(R.id.tv1);
		tv[2] = (TextView) this.findViewById(R.id.tv2);
		tv[3] = (TextView) this.findViewById(R.id.tv3);
		tv[4] = (TextView) this.findViewById(R.id.tv4);
		tv[5] = (TextView) this.findViewById(R.id.tv5);
		tv[6] = (TextView) this.findViewById(R.id.tv6);
		tv[7] = (TextView) this.findViewById(R.id.tv7);
		tv[8] = (TextView) this.findViewById(R.id.tv8);
		tv[9] = (TextView) this.findViewById(R.id.tv9);
		tv[10] = (TextView) this.findViewById(R.id.tv10);
		viewActionBar = (LinearLayout) this.findViewById(R.id.viewActionBar);
		iv_head = (ImageView) this.findViewById(R.id.iv_head);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
	}

	private void setEvents() {
		ll[3].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toEditValueActivity("手机号码", "phone", obj.getMust().getPhone());
			}
		});
		ll[7].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toEditValueActivity("办公电话", "office_phone", obj.getMust().getOffice_phone());
			}
		});
		ll[8].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toEditValueActivity("邮政编码", "code", obj.getMust().getCode());
			}
		});
		ll[9].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toEditValueActivity("单位及职务", "job", obj.getMust().getJob());
			}
		});
		ll[10].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				toEditValueActivity("单位地址", "address", obj.getMust().getAddress());
			}
		});
	}

	private void toEditValueActivity(String title, String key, String value) {
		Intent it = new Intent();
		it.setClass(this, EditValueActivity.class);
		it.putExtra("title", title);
		it.putExtra("key", key);
		it.putExtra("value", value);
		startActivityForResult(it, 101);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 101){
			if(resultCode == 200){
				reloadObj();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	};

	PersonalInfo obj;

	public void setData(PersonalInfo obj) {
		this.obj = obj;
		PersonalInfo.MustPart m = obj.getMust();
		if (m != null) {
			tv[0].setText(m.getNumber());
			tv[1].setText(m.getName());
			tv[2].setText(m.getPhone());
			tv[3].setText(m.getGroup());
			tv[4].setText(m.getCard());
			tv[5].setText(m.getParty());
			tv[6].setText(m.getOffice_phone());
			tv[7].setText(m.getCode());
			tv[8].setText(m.getJob());
			tv[9].setText(m.getAddress());
		}

		tv[10].setText(null);
	}

	private void loadData() {

		reloadObj();
	}

	ObjTask objTask;

	public void reloadObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(getActivity());
		objTask.execute();
	}

	private class ObjTask extends ExceptionAsyncTask<Object> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public Object doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().base_personal();
		}

		@Override
		public void onPostExecuteX(Object result) {
			if (result == null) {
				return;
			} else {
				setData((PersonalInfo) result);
			}
		}
	}

	@Override
	public void onDestroy() {

		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}